package com.itheima.health.controller;


import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.MessageCodeFormatter;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("传智健康检查项接口文档API")
@RestController
public class CheckItemController {

    @Reference
    private CheckItemService checkitemService;

    @GetMapping("/checkitem/findAll")
    @ApiOperation(value = "删除检查项", notes = "根据检查项id 逻辑删除检查项")
    public Result findAll(){
        try {
            List<CheckItem> list =checkitemService.list();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @PostMapping("/checkitem/findPage")
    @ApiOperation(value = "分页查询", notes = "根据分页条件查询分页数据")
    public Result findPage(@RequestBody QueryPageBean pageBean){
        try {
            PageResult pageResult=checkitemService.findPage(pageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("/checkitem/add")
    public Result add(@RequestBody CheckItem checkItem){
        //  checkItemService.add(checkItem);
        try {
            //   checkItem  有无 ID  mp帮助我们自动完成添加或修改操作   如果实体类存在 id   update 操作 否则 插入操作
            checkitemService.saveOrUpdate(checkItem);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }

    }

    @DeleteMapping("checkitem/deleteCheckItemById")
    public Result deleteCheckItemById(@RequestParam("id") Integer id){
        try {
            //  update  xxx  set is_delete = 1 where id = 1
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1); //  修改字段值
            checkitemService.updateById(checkItem);
            return  new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }

    }

}
